<?php

namespace App\Http\Controllers;

use App\Models\Chapter;
use App\Models\Content;
use App\Models\Novel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Log;

class NovelController extends Controller
{

    /**
     * 小说列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function novel_list(Request $request){
        $cookie['chapter_id'] =  $request->cookie('chapter_id')??null;
        $cookie['novel_id'] =  $request->cookie('novel_id')??null;
        $novel_list = Novel::get(['id','title','author']);
        return view('novel_list',['novel_list'=>$novel_list, 'cookie'=>$cookie]);
    }

    /**
     * 章节列表
     * @param $novel_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function chapter($novel_id){
        $chapters = Chapter::where('novel_id',$novel_id)
            ->orderBy('id')
            ->simplePaginate(73);
        return view('chapter_list',['chapter_list'=>$chapters, 'novel_id'=>$novel_id]);
    }

    /**
     * 小说内容
     * @param $chapter_id
     * @param $novel_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function content($chapter_id, $novel_id){
        Cookie::queue('chapter_id', $chapter_id, 86400);
        Cookie::queue('novel_id', $novel_id, 86400);
        $content = Content::where('chapter_id',$chapter_id)
            ->first()
            ->content;
        $min_chapter = Chapter::where('novel_id', $novel_id)->min('id');
        $max_chapter = Chapter::where('novel_id', $novel_id)->max('id');
        return view('content',[
            'content'=>nl2br($content),
            'chapter_id'=>$chapter_id,
            'min_chapter'=>$min_chapter,
            'max_chapter'=>$max_chapter,
            'novel_id'=>$novel_id,
            ]);
    }

}
